package net.orange.framework.validator;

import net.orange.architecture.validator.IObjectValidator;
import net.orange.framework.model.Menu;

/**
 * Validation : The menu code should follow the special format: 1)The menu code
 * must start with the superior menu code. 2) The menu code must be the format:
 * the superior menu code + \"-XX\"(XX is number).
 */
public class MenuCodeValidator extends FrameworkValidator implements
		IObjectValidator {
	/**
	 * The target menu.
	 */
	private Menu menu;

	/**
	 * Disabled the default contractor.
	 * 
	 * @exception
	 */
	@SuppressWarnings("unused")
	private MenuCodeValidator() throws Exception {

	}

	/**
	 * Constructor with group object to initialize the validation.
	 * 
	 * @param menu
	 *            The target menu.
	 * @exception
	 */
	public MenuCodeValidator(Menu menu) throws Exception {
		this.menu = menu;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.orange.architecture.validator.ObjectValidator#check()
	 */
	public boolean check() throws Exception {
		return false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.orange.architecture.validator.ObjectValidator#getMessage()
	 */
	public String getMessage() {
		return "";
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.orange.architecture.validator.IObjectValidator#validate()
	 */
	public String validate() throws Exception {

		if (menu.getLevel() != 1 && (menu.getParent() == null)) {
			return message.get("error2_menu_01");
		}

		if (menu.getParent() != null) {

			if (!menu.getMenu_code()
					.startsWith(menu.getParent().getMenu_code())) {
				return message.get("error2_menu_02");
			}

			if (menu.getParent().getLevel() >= menu.getLevel()) {
				return message.get("error2_menu_03");
			}

			String s = menu.getMenu_code().substring(
					menu.getParent().getMenu_code().length(),
					menu.getMenu_code().length());

			if (s == null || s.length() != 2) {
				return message.get("error2_menu_04");
			}
		}
		return null;
	}
}
